package com.glink.manage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.attendance.AttendanceAddDTO;
import com.glink.manage.dto.attendance.AttendancePageDTO;
import com.glink.manage.dto.attendance.AttendancePageDateDTO;
import com.glink.manage.service.AttendanceService;
import com.glink.manage.vo.attendance.AttendancePageDateVO;
import com.glink.manage.vo.attendance.AttendancePageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/API/Manage/attendance")
@Api(tags = "出勤管理")
public class AttendanceController extends AbstractBaseController{

    @Autowired
    public AttendanceService attendanceService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.第三方设备插入", notes = "01.第三方设备插入")
    @PostMapping(value = "/addAttendance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addAttendance(@RequestBody @Validated AttendanceAddDTO attendanceAddDTO) throws GeneralSimpleException {
        log.info("01.第三方设备插入:{}", attendanceAddDTO );
        boolean status = attendanceService.addAttendance(attendanceAddDTO);
        log.info("01.第三方设备插入: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "02.出勤信息分页查询-主", notes = "2.出勤信息分页查询-主")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findAttendancePage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<AttendancePageVO>> findAttendancePage(@RequestBody @Validated AttendancePageDTO attendancePageDTO) throws GeneralSimpleException {
        log.info("02.出勤信息分页查询-主:{}", attendancePageDTO );
        IPage<AttendancePageVO> attendancePage = attendanceService.findAttendancePage(attendancePageDTO);
        log.info("02.出勤信息分页查询-主: success");
        return this.response(attendancePage);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "03.根据id分页查询-详情", notes = "2.根据id分页查询-详情")
    @PostMapping(value = "/findAttendancePageDate", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<AttendancePageDateVO>> findAttendancePageDate(@RequestBody @Validated AttendancePageDateDTO attendancePageDateDTO) throws GeneralSimpleException {
        log.info("03.出勤信息分页查询-详情:{}", attendancePageDateDTO);
        IPage<AttendancePageDateVO> attendancePageDateVOIPage = attendanceService.findAttendancePageDate(attendancePageDateDTO);
        log.info("03.出勤信息分页查询-详情: success");
        return this.response(attendancePageDateVOIPage);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "04.导出出勤信息-主页面-项目部", notes = "4.导出出勤信息-主页面-项目部")
    @PostMapping(value = "/exportFindAttendancePage", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> exportFindAttendancePage(@RequestBody @Validated AttendancePageDTO attendancePageDTO) throws GeneralSimpleException , IOException{
        log.info("04.导出出勤信息-主页面-项目部:{}", attendancePageDTO);
        ByteArrayOutputStream outputStream = attendanceService.exportFindAttendancePage(attendancePageDTO, "Unit");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=barriers_info.xlsx");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        log.info("04.导出出勤信息-主页面-项目部: success");
        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "05.导出出勤信息-主页面-参建单位", notes = "5.导出出勤信息-主页面-参建单位")
    @PostMapping(value = "/exportFindAttendancePageUnit", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> exportFindAttendancePageUnit(@RequestBody @Validated AttendancePageDTO attendancePageDTO) throws GeneralSimpleException , IOException {
        log.info("05.导出出勤信息-主页面-项目部:{}", attendancePageDTO);
        ByteArrayOutputStream outputStream = attendanceService.exportFindAttendancePage(attendancePageDTO, "noUnit");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=barriers_info.xlsx");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        log.info("05.导出出勤信息-主页面-项目部: success");
        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "06.导出出勤信息-日期详情", notes = "5.导出出勤信息-日期详情")
    @PostMapping(value = "/exportFindAttendancePageDate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> exportFindAttendancePageDate(@RequestBody @Validated AttendancePageDateDTO attendancePageDateDTO) throws GeneralSimpleException , IOException{
        log.info("06.导出出勤信息-日期详情:{}", attendancePageDateDTO);
        ByteArrayOutputStream outputStream = attendanceService.exportFindAttendancePageDate(attendancePageDateDTO);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=barriers_info.xlsx");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        log.info("06.导出出勤信息-日期详情: success");
        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    }




}
